import {examineSubmitApi,examineBadRecordApi,examineBadInfoApi,examineRewardApi,examineDynamicListApi,reportList,examineCommentList,getAuditDataApi} from '@/api/examine.js'
 export default {
	data(){
		return{
			time:null,		//倒计时
			ifTimeout:false,	//是否超时
			startTime:'',		//倒计时开始时间
			submitData:{					//  提交数据
				checkTime:0,				//  integer	审核
				content:'',					//	string	封禁理由 加精理由
				notPassContent:'',			//	string	不通过理由
				great:2,					//	integer	加精 0 封禁 1
				id:0,						//	integer	文章id
				isPass:0,					//  integer	审核是否通过 0 不通过 1 通过
				type:'',					//	string	类型 article appeal comment
			},
			examineNum:0,
			examineInfo:{
				lastIllegal:false,
				count:0,
			},
			tipsShow: false, //提示弹窗控制
			ifShowFrequency: false, //是否显示次数
			tipsPopupText: '超过3分钟未进行操作，审核已自动结束，本次审核将记录为「不良审核记录」，「不良审核记录」超过三次则取消审核员资格。',
			reward:0,
			recode:[],
			todayNum:0,
			ifSubmitTimeout:false,		//是否违规退出超时
		}
	},
	watch: {
	    recode: {
	        handler (newValue, oldValue) {
	            if(newValue.length === 0){
					uni.removeStorageSync('submitTime');
				}
	        },
	        // immediate: true
	    }
	},
	onLoad(){
		this.getList()
		this.timeoutFun()
		this.examineInfoData()
		// 单页禁止测滑返回
		// #ifdef APP-PLUS
		   let currentWebview = this.$mp.page.$getAppWebview() //获取当前页面的webview对象
		   currentWebview.setStyle({ popGesture: 'none' })
		// #endif
	},
	onBackPress() {
		clearTimeout(this.time)
	},
	onUnload(){
		clearTimeout(this.time)
	},
	methods:{
		async getList() {
			try{
				uni.showLoading({
					title:'',
					mask:true
				})
				let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
				let res = ''
				switch (currentRoute){
					case 'pages/examine/dynamic':
						res = await examineDynamicListApi()
						this.submitData.type = 'article'
						this.submitData.id = res.data.length !== 0 ? res.data[0].dynamicId : 0
						break;
					case 'pages/examine/comment':
						res = await examineCommentList()
						this.submitData.type = 'comment'
						this.submitData.id = res.data.length !== 0 ? res.data[0].comment.id : 0
						break;
					case 'pages/examine/report':
						res = await reportList()
						this.submitData.type = 'appeal'
						this.submitData.id = res.data.length !== 0 ? res.data[0].appealId : 0
						break;
					default:
						break;
				}
				this.recode = res.data
				uni.hideLoading()
				if(!res.data.length){
					uni.showToast({
						title:'暂无数据，请结束审核',
						icon:'none'
					})
				}
			}catch(e){
				uni.hideLoading()
				uni.showToast({
					title: e.message || e.data,
					icon: 'none'
				});
				if(e.code === 10602){
					setTimeout(()=> {
						// uni.navigateBack()
						uni.reLaunch({
							url:'/pages/user/user'
						})
					}, 800);
				}
				//TODO handle the exception
			}
		},
		//3分钟倒计时
		timeoutFun(){
			this.startTime = new Date().getTime()
			this.time = setTimeout(()=> {
				this.ifTimeout = true
				this.badRecord()
				this.tipsShow = true
				uni.removeStorageSync('submitTime');
			}, 180000 );
		},
		
		//获取剩余违规次数及上次是否违规，每条审核元石奖励
		async examineInfoData(){
			try{
				let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
				//获取今日审核数据
				const todayData = await getAuditDataApi()
				switch (currentRoute){
					case 'pages/examine/dynamic':
						//今日审核动态
						this.todayNum = todayData.data.DayCheckArticles
						break;
					case 'pages/examine/comment':
						//今日审核评论
						this.todayNum = todayData.data.daycheckComments
						break;
					case 'pages/examine/report':
						//今日审核举报
						this.todayNum = todayData.data.DayCheckAppeals
						break;
					default:
						break;
				}
				
				const res = await examineBadInfoApi()
				this.examineInfo = res.data
				res.data.count > 3 ? this.ifShowFrequency = false : this.ifShowFrequency = true
				const rewardRes = await examineRewardApi()
				this.reward = rewardRes.data
				if (uni.getStorageSync('submitTime') && ((new Date().getTime()) - uni.getStorageSync('submitTime') > 180000)) {
					this.ifSubmitTimeout = true
					await this.badRecord()
					this.tipsShow = true
					this.tipsPopupText = '系统判断上次为非正常审核，该次审核将记录为「不良审核记录」，「不良审核记录」超过三次则取消审核员资格。'
				}
				// if (uni.getStorageSync('ifViolation') === true) {
				// 	this.tipsShow = true
				// 	this.tipsPopupText = '系统判断上次为非正常审核，该次审核将记录为「不良审核记录」，「不良审核记录」超过三次则取消审核员资格。'
				// }
				// uni.setStorageSync('ifViolation', true);
				uni.setStorageSync('submitTime', new Date().getTime());
				uni.setStorageSync('path', currentRoute);
			}catch(e){
				//TODO handle the exception
			}
		},
		
		//提交
		async submitFun(){
			clearTimeout(this.time)
			uni.setStorageSync('submitTime', new Date().getTime());
			try{
				this.submitData.checkTime = parseInt((new Date().getTime() - this.startTime) / 1000)
				const res = await examineSubmitApi (this.submitData)
				this.examineNum = this.examineNum + 1
				uni.showToast({
					title:'提交成功',
					icon:'none'
				})
				setTimeout(()=> {
					this.getList()
					this.timeoutFun()
				}, 800);
				
			}catch(e){
				//TODO handle the exception
				uni.showToast({
					title: e.message || e.data,
					icon: 'none'
				})
				if(e.code === 11000){
					setTimeout(()=> {
						this.getList()
						this.timeoutFun()
					}, 800);
				}
			}
		},
		
		//add不良审核记录
		async badRecord(){
			try{
				const res = await examineBadRecordApi()
				// await this.examineInfoData()
			}catch(e){
				//TODO handle the exception
			}
		},
		
		//超时提示弹窗确定
		timeoutTipsConfirm(){
			if(this.examineInfo.count <= 3){
				if (this.ifSubmitTimeout) {
					this.tipsShow = false
				} else{
					uni.navigateBack()
				}
			}else{
				uni.navigateBack()
				uni.removeStorageSync('submitTime');
			}
		},
		
		//导航栏左侧点击
		navLeftClick(){
			this.recode.length !== 0 ? this.badRecord() : uni.setStorageSync('ifViolation', false)
		},
		// 结束审核
		confirmOver() {
			try{
				this.isShowOver = false
				uni.removeStorageSync('submitTime');
				uni.removeStorageSync('path');
				uni.navigateBack()
			}catch(e){
				//TODO handle the exception
			}
			
		},
		
	},
 }